1 Introduction
A few months into using for data analysis, I had a lightbulb moment—I didn’t actually know what was. Sure, I could write some code and get results (output), but that didn’t mean I truly understood what could do or how powerful it really is. I realized I never had a proper introduction to the language.
Often, experts skip over details they assume are ‘obvious’—but things are only obvious once you’re familiar with them. In my case, I felt like I’d been thrown into the deep end with , missing the foundational understanding that would have made everything else click.
Despite this rocky start, my passion to learn didn’t wane. Over time, I’ve come to see how much the way you’re introduced to shapes your experience, especially if you’re used to point-and-click software (GUIs) and are now stepping into the world of command-line interfaces (CLIs) and scripting.
That’s why, after navigating my own struggles, I decided to write this comprehensive guide. It’s meant for absolute beginners (and if the term ‘absolute beginner’ feels a little harsh, I apologize—but it answers the question: Who is this for?). I hope it will also help those who already have some experience but feel like they missed out on key concepts during their own introductions to the language.
I won’t promise that you’ll become an programming guru overnight after reading this guide, but by the end of it, you should be able to:
Install and set up an IDE (Integrated Development Environment) to work in.
Run R commands and use the language with confidence.
Load and utilize packages effectively.
Import data into the environment.
Clean and manipulate data for more efficient analysis.
Understand basic data types and structures in e.g., vectors, lists, and data frames.
Write your own R functions and scripts.
Use the power of to perform some basic statistical analysis and data-processing tasks.
Appreciate the meaning of the following code:
invisible(
lapply(
pkgs,
library
)
)Create beautiful graphs and visualizations to bring your data to life.
Know where to find additional help and resources to improve your coding skills.
2 Overview of
2.1 What is ?
At first glance, “What is ?” may seem like a simple question, but as Zuur, Ieno, and Meesters (2009); point out, it’s not so easily answered—and I agree. Broadly defined, is a computer language that allows users to program algorithms and use tools that others have developed. This is a fairly generalized description and could apply to the plethora of programming languages, but stands out because it’s often defined as a powerful environment specifically designed for statistical computing and graphics—or more simply, a statistical programming language.
It was developed by Ross Ihaka and Robert Gentleman as a free software environment for teaching, while they were colleagues at the University of Auckland in New Zealand (Vries and Meys 2012). Their goal was to create an intuitive system for statistical computing and data visualization—something that statisticians across the world could adopt with ease. Given its origins, is exceptionally well-suited for data science, a vital field in today’s world— developed by statisticians for statisticians. In fact, is often referred to as the “lingua franca of statistics” among statisticians and scientists.
A more practical way to understand , however, is by looking at what it can do. As I’ve often told colleagues and friends, “ can do anything you can imagine,” and that’s hardly an exaggeration. With , you can:
Write custom functions and perform calculations
Apply nearly every available statistical technique
Generate everything from simple to highly complex graphs
Perform geocomputation and geostatistics (Lovelace, Nowosad, and Muenchow 2019)
Create stunning 3D visualizations (Morgan-Wall 2024)
Develop your own libraries of reusable functions
Perhaps one of the most significant, nontrivial reasons for its widespread use is that is free of charge, supported by a vast, active user community.
So Why Isn’t Everyone Using
This question is easier to answer: has a notoriously steep learning curve. Since it relies heavily on programming, even though there are graphical user interfaces (GUIs) available, none are advanced enough to fully eliminate the need for coding. But once you get the hang of , I reckon you’ll find it hard to return to any other software for statistical computing.
From my own experience—and from hearing others share theirs—learning both and statistics simultaneously can feel overwhelming, like climbing two steep hills at once. Zuur, Ieno, and Meesters (2009), also acknowledge this issue, noting that when is taught alongside statistics, many learners tend to focus on simply getting the code to run, and often overlooking the weightier statistical concepts behind it. I faced the same struggle when I was first introduced to .
That’s why this post focuses primarily on providing basic instructions and not diving too deeply into statistics. However, I will demonstrate some fundamental statistical procedures you can perform in later in the post. If your goal is to learn both R and statistics, this guide will provide the foundation you need to master , equipping you with the skills to explore its powerful statistical tools and the vast range of packages that extend its capabilities.
2.2 Historical Background of
is a modern implementation, or dialect, of an earlier programming language called S (Becker, Chambers, and Wilks 2018). It also incorporates lexical scoping semantics inspired by Scheme The development of began in 1991, when Professors Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, recognized the need for a better software environment for their Macintosh teaching laboratory. Their journey into developing is detailed in a 1996 paper in the Journal of Computational and Graphical Statistics (Ihaka and Gentleman 1996).
The name “R” is a nod to both the first names of its authors (Ross and Robert) and a playful reference to its predecessor, S. By 1993, after they had made significant progress, Ihaka and Gentleman began to share their work more widely, distributing binary copies of `{{< fa brands r-project >}} through the StatLib data archive and announcing it on the s-news mailing list.
Interest in quickly grew, and many began to collaborate with the duo—most notably, Martin Mächler from ETH Zurich. In June 1995, Mächler convinced Ihaka and Gentleman to release as free and open-source software under the terms of the Free Software Foundation’s GNU General Public License, allowing anyone to use and modify it freely. Initially, bug reports were sent via email, but as interest surged, it became clear that manually maintaining communication was unsustainable. In response, in March 1996, Mächler volunteered the facilities at ETH Zurich to set up the r-testers mailing list, which allowed for automated discussions about and its development.
Approximately a year later, r-testers was replaced by three newsgroups: R-announce, R-help, and R-devel. Over time, more mailing lists were added to accommodate the growing community. Currently, the project maintains five general mailing lists, including R-package-devel and R-packages, which can be found at https://www.r-project.org/mail.html.
Today, the R Core Team—a group of currently 18 active individuals—has write access to the source code (https://www.r-project.org/contributors.html), guiding its ongoing development. Many others have also contributed through new code submissions and bug fixes, making R a true collaborative effort. For those interested in a more detailed account of R’s development, Ross Ihaka provides a fascinating overview, available at http:// cran.r‐project.org/doc/html/ interface98‐paper/paper.html.
The Comprehensive R Archive Network (CRAN) was officially launched on 23 April 1997, providing a centralized platform to store R’s executable files, source code, documentation, and packages. Over time, CRAN has grown into one of the most extensive repositories for data science tools. In April 2003, the R Foundation was founded as a non-profit organization to provide long-term support for ’s continued development, ensuring that it remains a leading tool for statistical computing and data visualization.
Since its humble beginnings, has grown into a global force in statistical computing and data science. Thanks to its open-source nature and a thriving community, it remains one of the most widely used and rapidly evolving tools for data analysis. As the field of data science expands, ’s importance continues to grow, cementing its place as an indispensable resource for statisticians, data scientists, and researchers alike.
3 Getting Started With Programming
Every journey begins with gathering the right tools, and your adventure into programming is no different. The first essential step for any novice is setting up the environment to start coding. Essentially, you have two main options:
Run
in a browser, for example, via RStudio Cloud.
While installing along with an IDE like RStudio, VS Codeor Positron requires a little more time and effort, it offers a more robust setup—allowing offline work and leveraging your computer’s CPU—I highly recommend this approach, especially for those committed to diving deep into . However, if you’re just exploring or testing the waters, running online through platforms like RStudio Cloud may be more convenient (provided you have a reliable internet connection).
Whichever path you choose, this post is designed to accommodate you. Whether you install locally or use an online platform, the examples and instructions provided here will guide you seamlessly through your programming journey.
3.1 Downloading and Installing
Now that we’ve covered an overview of and some of its history, it’s time to get your own copy of so you can start using it. The easiest way to do this is by downloading a precompiled binary distribution of R, which includes the base system and contributed packages, and installing it on your computer. While it’s also possible to install from source, this is more complex and generally not necessary for most users. I recommend sticking to the precompiled binaries, which are easy to install and are available for a range of operating systems, including Windows, macOS, and Linux. In this guide, we’ll focus on installing R on Windows and macOS.
3.1.1 Installing on Windows
Installing on Windows is a straightforward process. Here’s a step-by-step guide:
Go to the CRAN website: Visit the Comprehensive R Archive Network (CRAN) at https://cran.r-project.org/.
Download R for Windows: Click on the “Download R for Windows” link.
Download the base system: On the next page, click “base” or “Install R for the first time” to download the R executable file (e.g.,
R-4.x.x-win.exe).Run the R installer: Once downloaded, double-click the executable file to start the installation process. You may be prompted to allow the application to make changes to your device—click “Yes” to continue.
Select installation language: The installer will first prompt you to select the installation language. Choose your preferred language and click “OK”.
- License agreement: You’ll see a window with the GNU General Public License information. Simply click “Next”.
- Choose the destination location: By default, R will install in
C:\Program Files\R\R-4.x.x. Unless you have a specific reason to change it, I recommend leaving this as is and clicking “Next”.
- Select components: In the “Select Components” window, the default options should suffice for most users, so just click “Next”.
- Startup options: Again, leave the default startup options selected and click “Next”.
- Select Start Menu folder: The default Start Menu folder is fine, so just click “Next” here as well.
- Additional tasks: The installer will ask if you’d like to create desktop shortcuts or add
Rto your system PATH. The default options are usually best, so leave them checked and click “Next”.
- Begin installation: After confirming all the settings, click “Next” to start the installation process.
- Complete installation: Once the installation is complete, click “Finish” to close the installer.
Congratulations, you’ve successfully installed on your Windows system!
3.1.2 Installing on macOS
The installation process for macOS is very similar to that of Windows, but with a few differences in file format. Follow these steps:
Go to the CRAN website: Head over to https://cran.r-project.org/.
Download R for macOS: Click on “Download R for macOS”.
Choose the latest release: In the “Latest release” section, download the appropriate version of for your macOS system.
Open the installer: Once the download is complete, double-click the
.pkginstaller file to open it.Follow the installation prompts: Click “Continue” and proceed through the installer by following the on-screen instructions.
Complete installation: Once the installation process finishes, click “Close” to exit the installer.
You now have installed on your macOS system!
With these steps completed, you’re ready to start programming in ! Next, we’ll move on to setting up a suitable Integrated Development Environment (IDE) to enhance your experience with .
3.2 Exploring IDE Options for : Elevate Your Programming with RStudio, VS Code, or Positron
3.2.1 Getting Started With the Interface
When working with , several user interfaces (UIs) are available, but every installation of includes a basic/standard interface—sometimes called the “R Console.” While you may eventually prefer using an advanced IDE like RStudio, it’s important to familiarize yourself with this standard interface. Not only can it be helpful when using a machine without your preferred or favorite interfaces, but it also serves as a good foundation for understanding how other interfaces are organized. You may even come to appreciate and enjoy the simplicity of the standard interface.
What follows is a practical guide on using the RGUI, which is the standard graphical interface for on Windows platforms.
On Windows, is typically installed in C:\Program Files\R, and you can access it from the Start menu under the group. If, during installation, you selected “Create a desktop shortcut,” you’ll also have an RGUI icon on your desktop. To launch , simply double-click this icon, and you’ll be greeted by a user interface that looks something like this:
When opened, the main window, labeled “R Console,” will appear. This is where you will enter and execute your commands. You can type commands directly into the Console and see the output immediately. This is also where error messages appear, . Keep in mind, closing the Console will end your session.
The main window of RGUI is the “R Console,” which automatically opens when you launch . This Console is where you enter commands one at a time. Depending on the command, the output will be displayed in the Console, and this is also where any error messages will appear. The Console is unique and it’s the primary space for interacting with —keep in mind, closing the Console will end your session.
You might hear me refer to as if it were a person, saying things like “Tell to do this” or “Tell to do that.” But, of course, can’t do anything—it’s just a language. This is simply shorthand for saying, “Instruct your computer to perform a task by writing a command in the language in the R console.” It’s your computer, not itself, that does the heavy lifting.
Is this shorthand a bit imprecise and lazy? Yes. But does everyone still use it? Absolutely—because it’s convenient and saves time!
For new users, the easiest way to save the Console’s contents is to navigate to File → Save to file from the menu. However, note that the Console only retains the last few thousand lines written to it, so it’s wise to save important work elsewhere. You may also find it helpful to clear the Console to start fresh. To do this, simply press Ctrl + L or select Edit → Clear console from the menu.
Ready to Write Your First Program in
Now that we have installed and running, let’s dive right in by writing our first simple computer program. To do this, open (if it’s not already open), and in the “R Console” type the following code after the > prompt symbol, then press Enter↵:
print("Hello, World!")After pressing Enter↵, you should see the following output appear in the Console, as shown in the image below.
Congratulations!—you’ve just written a computer program! A computer program is simply a sequence of instructions that perform a specific task when executed by the computer. The “Hello, World!” program is a classic way to introduce novice programmers to a new language. It serves two purposes: demonstrating the basic syntax of the language and verifying that the installation was successful, ensuring you know how to use it. In this case, the program consists of just one instruction—the command to display the text “Hello, World!” in the console. The command responsible for this is print().
A couple of things to note about your first program:
“Hello, World!” is a string: A string is a way of representing text in a computer program. In , a string must be enclosed in quotes (
' 'or" "), which act as delimiters, signaling where the string starts and ends. The text inside the quotes—Hello, World!—is the actual string.printis a function: Just like in mathematics, a function in takes input, performs an operation, and returns output. Theprintfunction’s job is to take its input (in this case, the string"Hello, World!") and display it in the console in a readable format.Calling a function: To execute the function, you need to “call” it, which is done by following the function name (
print) with a pair of parentheses. The input to the function, in this case, the string, goes inside the parentheses. The parentheses are important as they tell where the input starts and ends, serving as another type of delimiter.
Let’s try another simple program with a basic arithmetic calculation—yes, can function like a calculator. You can:
Add numbers with
+Subtract with
-Multiply with
*Divide with
/Raise numbers to a power with
^
Type the following code into the console and press Enter↵:
print(4 + 2)After pressing Enter↵, you should see the following output in the console:
So far, we’ve been using the print() function to display output. However, you can actually get output in the console without explicitly calling print(). You don’t always need the print() function for simple outputs. Typing just the expression will display the result:. For example, suppose you type:
"Hello, World!"In the console, you’ll see the following result:
Similarly, you can try:
2 + 2with the output:
So far, you’ve been using in what computer scientists call interactive mode. This means you type a command into the console, R immediately reads it, evaluates it, and prints the result back to the console for you to see. Then, you type the next command, and the process repeats. This cycle is known as the Read-Evaluate-Print Loop, or REPL for short.
, like other interactive languages, excels at providing immediate feedback, which makes it especially useful for statisticians and data analysts who often run small procedures, check results, and refine their approach iteratively—after all, R was originally designed by statisticians, for statisticians. As beginners, the REPL offers a fantastic way to observe ’s behavior and receive instant feedback on the simple programs we’re writing, helping us quickly understand how R processes our commands.
In some languages, like C, Java, or FORTRAN, you need to compile your human-readable code into machine-readable code (typically in the form of 1s and 0s) before it can be executed. If you’ve worked with those languages before, you might wonder whether you need to compile your code too. The answer is no—R is a dynamic programming language, meaning it automatically interprets your code on the fly as you run it, without the need for a separate compilation step.
Below I have included an interactive webR REPL console for more engagement right here on this page especially for those who may only want to test the water’s of . Feel free to execute the examples we’ve doe above.
- Input code in the input box and click Run to send the command to the interactive console.
The “R Editor” Window:
For more complex programs, you’ll want to work in the “R Editor,” where you can write and edit multiple lines of code. When you’re ready to move beyond typing and executing commands one by one, you can use the “R Editor” window to write and manage scripts efficiently. To open a blank editor, click File → New Script from the menu. If you have an existing script, select File → Open Script to continue working on it. The image below shows a new blank “R Editor” window.
You can have multiple “R Editor” windows open simultaneously, allowing you to work on different scripts side by side.
The available menus and toolbar options change based on which window is currently in focus. In the image above, the focus has shifted from the “R Console” to the “R Editor,” which is why the toolbar and menu options have updated accordingly.
As expected, the script editor allows you to run commands either individually or in groups. You can also execute part of an expression by highlighting the code and pressing Ctrl + R (or using the “Run line or selection” button in the toolbar, or clicking Edit → Run line or selection from the menu).
To run a single line of code, place the cursor within the line you want to execute and press
Ctrl + R. Note that one line may not always be a complete executable statement.To run a specific section of code, highlight the desired text and press
Ctrl + R. The highlighted text can span multiple lines or just part of one.To run the entire script, highlight everything using
Ctrl + Aand then pressCtrl + R.
You can save your script anytime by pressing Ctrl + S or selecting File → Save.
The “R Graphics” Window:
When you utilize a function such as plot() to create a graphic, the graphics window—commonly referred to as the “device”—serves as the default output area. If a graphics window is not already open, a new one is produced, otherwise the existing window is reused.
To illustrate this, let’s plot a graph using a built-in dataset called pressure. The dataset, which comes pre-installed with , shows the relationship between temperature (in degrees Celsius) and vapor pressure of mercury (measured in millimeters). Below, we apply the plot() function to the pressure dataset and execute it from the “R Editor”:
To open a new graphics window without closing an existing one, you can use the dev.new() function. This allows you to create multiple graphics windows for your visualizations. To save a graphic, simply click File → Save As and select your desired format from the menu.
The “Data Editor” Window:
The simplest way to examine the contents of a basic data object is by printing it directly to the Console. Just type the object’s name at the command prompt.
To explore the data within a data frame or matrix, you can use either fix(dataframe) or View(dataframe), where “dataframe” refers to the name of your data object. These functions are specifically for data frames and matrices. The fix() function allows you to modify the data, while View() only displays it without editing capabilities. Alternatively, you can navigate to Edit → Data Editor in the menu and specify the name of the data frame or matrix you wish to edit.
As an example, let’s view the pressure dataset we used earlier to plot the graph. You can use the following command:
fix(pressure)This will open the data frame in a tab where you can inspect it interactively.
Any changes you make using the Data Editor are automatically saved to your workspace (you’ll learn more about saving the workspace later). However, while the Data Editor is open, you cannot run any other code, and files cannot be saved directly from this window.
The “R History” Window:
You can access your command history by using the history() function, which opens a window displaying your most recent commands. By default, this shows the last 25 lines of code you’ve submitted. Below is an example of what my current history looks like.
To save the contents of this history window, you can either click File → Save to file in the menu or use the savehistory() function directly in the Console. This allows you to preserve your previous commands for future reference or reuse.
Saving the Workspace
stores the data and objects you’re working with in an area of your computer’s memory called the “workspace.” While the standard R interface doesn’t have a dedicated window to display these objects, you can easily list them in the Console by using the ls() function. These objects can include data imported from external sources (such as text files), new data you’ve generated, results from expressions you’ve executed, and various other elements.
When you shut down R, it will typically prompt you to save your workspace. If you choose to save it under the default name .RData (note that it’s just the file extension with no filename), will automatically load this workspace the next time you launch it.
For substantial work, it’s generally considered good practice to save your raw data sources and a script that recreates your data objects. This ensures that you—or anyone else—can fully reproduce your analysis. From this perspective, it’s often unnecessary to save the workspace itself.
However, if your work involves lengthy data processing or a complex script, saving the workspace can be useful as it provides a convenient starting point for your next session, saving time and effort.
To save your workspace, either click File → Save workspace in the menu, or use the save.image() function in the Console. If you want to save it under a specific filename, use save(file = "filename"). You can load a previously saved workspace by running load(file = "filename").
So far, we’ve successfully interacted with base —we’ve written our first “Hello, World!” program, performed basic arithmetic, and even plotted a graph (how cool is that?). While RGUI is functional, it can feel somewhat basic and limited compared to modern integrated development environments (IDEs), which offer enhanced features for coding, debugging, and data visualization. In the next section, we will explore the powerful capabilities provided by IDEs.
3.2.2 RStudio IDE for and python
One of the most popular ways to interface with , beyond the basic RGUI, is through RStudio, a powerful Integrated Development Environment (IDE) designed primarily for . RStudio offers an intuitive and interactive environment, making coding, data analysis, and project management significantly easier. However, it’s important to note that before using RStudio, you must first install on your computer.
According to RStudio’s mission statement:
“To create free and open-source software for data science, scientific research, and technical communication. We do this to enhance the production and consumption of knowledge by everyone, regardless of economic means, and to facilitate collaboration and reproducible research, both of which are critical to the integrity and efficacy of work in science, education, government, and industry.”
RStudio was founded by J.J. Allaire, a software engineer and entrepreneur, in 2009, with the first version of RStudio IDE released in 2011. Since its release, RStudio has become a go-to tool for data scientists, researchers, and statisticians worldwide.
While RStudio is mainly known for its support of , it also accommodates other programming languages, such as Python, SQL, and even Stan for Bayesian statistics. This flexibility makes RStudio a multi-functional open-source IDE that is widely adopted for various programming and data analysis needs. RStudio works best with versions 3.0.1 and higher, ensuring compatibility with most modern workflows.
Visit Posit, the official RStudio website to download RStudio Desktop.
On the homepage, expand the
"FREE & OPEN SOURCE"Tab and look under the"Analyze & Explore"section.Click
"RSTUDIO IDE"(or simply select"DOWNLOAD RSTUDIO"at the bottom of the list. If you choose"DOWNLOAD RSTUDIO", you can skip to step 4).This will take you to the RStudio IDE page.
Choose between RStudio Desktop (free) and RStudio Desktop Pro (Paid).
On the RStudio IDE page, you will see two editions as you scroll down the page:
RStudio Desktop– Free (Open Source)RStudio Desktop Pro– Paid
For almost everyone—students, researchers, and individual users—the free
RStudio Desktophas all the features you need for R programming.The Pro version is essentially the same software but includes two additional benefits intended for organizations:
A Commercial License
Some companies cannot use software licensed under the AGPL (the license for the free version).
The Pro edition provides a commercial license that allows organizations to;- using RStudio in internal, closed-source, or proprietary environments
- avoid releasing modified source code
- comply with strict corporate software policies
Most individual users, students, and researchers do not need this.
Access to Priority Support
RStudio Desktop Pro includes:
- direct support from Posit engineers
- guaranteed response times
- assistance with installation, errors, and configuration
Users of the free version still have excellent community resources such as:
- Posit Community
- Stack Overflow
- Github issues
- online forums and tutorials
Go ahead and click on
"DOWNLOAD RSTUDIO DESKTOP"under theOpen Source Editionto get the free version of RStudio.This will take you to the DOWNLOAD RStudio Desktop page
Select
DOWNLOAD RSTUDIOOn the DOWNLOAD RStudio Desktop page selectDOWNLOAD RSTUDIO.This will take you to the RStudio Desktop installers page.
Download the appropriate RStudio Desktop installer for your machine.
On the RStudio Desktop installers page, you will see a short checklist before the actual installer download links. The first item is
"1: Install ", which is a reminder that must be installed before RStudio can be used.If you have not yet installed , clicking this will take you to CRAN (the official website), where you can download a precompiled binary of for your operating system.
If you have already installed , you can safely ignore this step and proceed directly to downloading RStudio.
For Windows users, clicking
"DOWNLOAD RSTUDIO DESKTOP FOR WINDOWS"will immediately start the download of the installer (.exefile).If you are using macOS or Linux, simply scroll down the page to find the appropriate installer for your operating system and follow the corresponding instructions.
Run the installer.
Once the download is complete, locate the installer file and launch it.
On Windows, double-click the
.exefile.On macOS, open the
.dmgfile and follow the on-screen instructions.
This will launch the RStudio installation wizard, starting with a welcome screen similar to the one shown below.
Proceed through the setup wizard by following the prompts. When you reach the “Choose Install Location” screen, it is strongly recommended to keep the default installation path unless you have a specific reason to change it. Click Install to continue.
RStudio will then begin installing the required files on your system.
Once the installation completes successfully, click Finish to exit the installer. RStudio Desktop is now installed and ready to use.
Launch RStudio and start working
Once installation is complete, launch RStudio Desktop from your applications menu or desktop shortcut. There’s no need to open the base application separately—RStudio automatically detects and uses the installation on your system.
As RStudio opens, you may briefly see a start-up process similar to the one shown below.
Once RStudio opens, you’ll be greeted by it’s main interface, as shown below:
From this point onward, you can work entirely within RStudio. The IDE is now fully connected to , providing a more powerful and user-friendly environment for writing code, running analyses, managing projects, and creating visualizations.
🎉 Congratulations!
You’re now fully set up and ready to start working with using the RStudio IDE.
Yes. Even if you plan to use RStudio, you must install on your computer.
RStudio is an Integrated Development Environment (IDE) that makes working with R easier, but it does not include R itself. Instead, it connects to and runs the version of installed on your system.
Before diving into coding, it’s worth spending a few moments customizing RStudio to enhance your experience (trust me, it makes a difference).To access the settings via Global Options, follow these steps:
On macOS, go to Tools → Global Options or simply press
⌘ + ,.On Windows, head to Tools → Global Options or press
Ctrl + ,.
You should be greeted with the Global Options window as below.
RStudio offers many customization options, and over time you’ll naturally adjust them to match your personal workflow. For now, I’ll highlight just a few essential settings that are especially helpful for beginners and will set you up for a smoother experience from the start.
Code Formatting (Code → Editing)
Start by improving how your code is structured and displayed.
In the Code → Editing tab, enable at least the first five options, paying special attention to “Auto-indent code after paste.”
This setting automatically fixes indentation when you paste code, helping your scripts stay clean and readable without extra effort. Good indentation isn’t just about aesthetics—it makes your code easier to follow, debug, and maintain, especially as your scripts grow longer or more complex.
If you’re just starting out, these options quietly enforce good coding habits in the background.
Code Visibility (Code → Display)
Next, make it easier to see where you are in your code.
In the Code → Display tab, consider enabling the first three options. One especially useful feature is “Highlight selected line.”
This highlights the line your cursor is currently on, which is incredibly helpful when working in long scripts. It reduces eye strain and helps you avoid running or editing the wrong line by mistake.
Appearance and Personalization (Appearance)
Finally, customize how RStudio looks and feels.
Under the Appearance section, you can choose an Editor Theme, which controls the color scheme of your code editor. This is one of the most noticeable and impactful changes you can make.
Take a moment to browse through the available themes and pick one that feels comfortable to you—there’s no “correct” choice here. Personally, I recommend trying a dark theme, especially if you code for long periods, as it can reduce eye strain. My go-to choice is Solarized Dark, but feel free to experiment and settle on what works best for your eyes.
Once you have selected your preferred options, click “Apply” to save the settings and apply the changes.
These small tweaks can make a big difference in your overall RStudio experience, giving you a smoother, more comfortable environment as you embark on your programming journey.
3.2.2.1 The RStudio Interface
The RStudio interface is divided into four quadrants, each serving a specific function:
The
Consolewindow,The
Sourcewindow,The
Environment | History | Connections | Tutorialwindow, andThe
Files | Plots | Packages | Help | Viewerwindow
While you can customize the layout of the panes in the RStudio interface, I personally prefer the default arrangement. To change the pane layout, go to Tools → Global Options. Under Global Options, select the Pane Layout option and adjust the panes to your liking.
You may notice that sometimes the Source window isn’t visible when you first open RStudio. This happens when there’s no file opened. To activate the Source window, either open an existing file or create a new one by selecting File → New File → R Script from the menu bar, or by using the keyboard shortcut Ctrl + Shift + N on Windows or ⌘ + Shift + N on macOS.
Now, let’s briefly explore the purpose of each pane and how they contribute to your workflow in .
3.2.2.1.1 The Console Window
The Console is normally located in the bottom-left pane of RStudio unless changed. This is where the engine you installed separately actually runs inside RStudio. In other words, it is the same R Console you would see if you opened base R on its own, but now embedded within the RStudio interface.
The Console displays the output of your commands and computations, and you can also type and execute R code directly here—just as you did earlier when working with base R.
Let’s try a quick example by calculating the sum of 20 + 7. Click into the Console, type the following, and press Enter/Return ↵ on your keyboard. The result should be straightforward:
# We type this directly into the console:
20 + 7Here’s an example of how it should appear in your RStudio Console:
In the top-right corner of the Console pane, you’ll notice a broom icon. This is an important feature for clearing the Console when it gets too cluttered. If you want to remove all the text in the Console, simply click the broom icon. Alternatively, you can use the handy keyboard shortcut Ctrl + L on both Windows and Mac for the same purpose. If you prefer using code, the following commands can also clear the Console:
# On Windows:
shell("cls")
# or
cat("\014")
# On Linux or macOS:
shell("clear")3.2.2.1.2 The source Window
Located in the top-left pane of RStudio, the Source Window serves as a versatile space for managing various types of files, including data, programming code, and notes. The term “source” encompasses any file you wish to work with. The Source panel can fulfill multiple functions, such as:
Inspecting Data: View datasets in a spreadsheet-like (Excel-style) format, making it easier to explore and understand your data.
Writing and Editing R Scripts: Create, open, and modify R scripts where you write code for your analyses and projects.
Working with Other Text-Based Files: The Source window can also open and edit a wide range of file formats, including:
Plain text files (
.txt)Markdown files (
.md)HTML documents (
.html)LaTeX files (
.tex)BibTeX files (
.bib)
Running Analyses: Code written in the Source window can be executed line by line or in chunks, allowing you to run analyses, generate outputs, and build results in a structured and reproducible way.
In essence, the Source window provides a workspace for any text-based file that RStudio can read. It is where most of your coding and project development will take place, making it one of the most important components of the RStudio interface. However, it’s important to note that Microsoft Office documents and non-text files (like images) are not supported in this pane. This limitation emphasizes the focus on coding and text editing within the Source window.
While you can type and run code directly in the Console, the Source window is strongly recommended for most work because it allows you to:
Save your code for future use
Reproduce your analyses exactly at a later time
Organize your work into scripts rather than relying on temporary command
Edit and review code before running it
In short, the Console is great for quick tests, but the Source window is where real, reusable work happens.
3.2.2.1.3 The Environment | History | Connections | Tutorial Pane
The top-right pane in RStudio is a multi-purpose area made up of several tabs, each designed to help you manage and understand your work more effectively.
The Environment tab shows all the objects currently available in your R session—that is, everything R has in memory and can work with. One of the first and most important objects you will create is a dataset, since data is central to nearly all analyses.
As you continue working in , this pane may also contain:
Datasets
Vectors
Lists
Functions you define
Other objects created during your analysis
If these terms are unfamiliar right now, don’t worry—we will introduce and explain each of them step by step in later sections. For now, it’s enough to know that the Environment pane acts like a dashboard, allowing you to quickly see what objects exist, inspect them, and manage your workspace.
The History tab keeps a record of the commands you have run during your current and past R sessions. This can be helpful if you want to revisit, reuse, or rerun previous code without typing it again. For example, if you computed the sum of 20 + 7, this entry will be stored in your history. You can quickly insert this command back into the console by clicking on it in the History pane and selecting To Console. This action will paste 20 + 7 back into the console, allowing you to hit Return ↵ to retrieve the result. Alternatively, you can copy the command into an existing or new R Script by clicking on To Source, enabling you to save and reuse it later. If you want to preserve your command history, you can click on the floppy disk icon to save it. Additionally, you can delete individual entries using the button with a white page and red circle, or clear the entire history using the broom icon (note that this action is irreversible).
The Connections tab is used when working with external data sources such as databases or remote storage systems. Beginners may not use this tab immediately, but it becomes useful when working with larger or more complex data sources.
Lastly, the Tutorial tab provides interactive, guided learning content. Some packages include tutorials that appear here, making it a convenient space for hands-on learning directly within RStudio.
3.2.2.1.4 The Files | Plots | Packages | Help | Viewer | Presentation Pane
The bottom-right pane of RStudio is made up of five important tabs, each designed to support different aspects of your workflow; from managing files to viewing plots and accessing help.
The Files tab displays all files and folders within your working directory—the default location where RStudio reads from and saves files such as scripts, data, and outputs. This tab acts like a built-in file browser, allowing you to navigate your folders without leaving RStudio.
Using the Files pane, you can:
Open and view files
Navigate to datasets you want to load into
Create, rename, or delete folders
Keep your work neatly organized
Later in this guide, we will explore R projects and working directories in more detail. For now, it’s helpful to think of the Files tab as your central navigation hub inside RStudio—keeping all the components of your work easy to find and manage in one place.
Because the Console is designed primarily for text input and output, it cannot display visualizations directly. Instead, RStudio offers the Plots tab, which is equivalent to the “R Graphics” window in base . This tab is specificzlly dedicated to rendering and displaying any graphs or charts you create.
Let’s walk through a simple example. Type the following code into your Console to generate a basic boxplot (remember to press Enter/Return ↵ to run the code)
# This creates a basic boxplot using the built-in 'mtcars' dataset
boxplot(mtcars$hp)The mtcars dataset comes pre-installed with R, making it convenient for quick demonstrations. In this example, the boxplot() function is used to visualize the distribution of the hp (horsepower) variable.Once you run the code, the resulting plot will automatically appear in the Plots tab of RStudio.
You can navigate through multiple plots by using the arrow buttons in this pane. To delete the current plot, click on the red circle with a white 'x', or clear all plots by using the broom icon. You can also export your plot for use in presentations or reports, with options for saving it as an image file or PDF.
The Packages tab is used to manage additional tools, known as packages, that extend R’s functionality. You can think of packages as apps for your phone. Each package adds new capabilities, such as creating advanced visualizations, performing machine learning analyses, or simplifying repetitive tasks.
By default, comes with a set of basic packages already installed, but many analyses require additional packages that you can install as needed. The Packages pane displays a list of all packages currently installed on your system. From here, you can load or unload packages and quickly check which tools are available for your work.
The Help tab is your primary go-to reference when you need assistance while working in , and it also provides quick access to official documentation, manuals, learning resources, and other useful reference materials related to and RStudio..
The Help tab also provides detailed documentation for functions, datasets, and packages. Whenever you are unsure how a function works, what arguments it accepts, or what a dataset contains, the Help pane is usually the first place you should look.
For instance, if you want to explore details about the built-in mtcars dataset used earlier, simply type the following command into the Console:
# Type a '?' followed by the name of a dataset/function/etc.
# to look up helpful information about it.
?mtcarsRunning this command opens the Help tab and displays comprehensive information about the dataset, including a description, variable definitions, and usage examples.
You can also use the search bar within the Help pane to look up specific topics or browse related documentation. Beyond RStudio, there is a rich ecosystem of online resources for learning and troubleshooting. For additional learning, I highly recommend CRAN’s ‘Getting Help with R’ webpage. And if you’re ever stuck, don’t hesitate to expand your search to other resources, such as Google, Stack Overflow or Twitter (using #RStats).
Lastly, the Viewer tab is designed to display dynamic and interactive content that cannot be shown in the static Plots tab. This includes HTML-based outputs such as interactive visualizations, web dashboards (for example, Shiny apps), maps, and even entire websites generated from R. Content shown here is often interactive, allowing you to interact with your results in real-time within RStudio. If needed, these outputs can also be opened in your default web browser for a larger or more flexible viewing experience.
Closely related is the Presentation tab, which is used when creating slide presentations directly from R using tools such as R Markdown or Quarto. When you render a presentation, the slides are displayed in this pane, allowing you to preview, navigate, and present your work without leaving RStudio. This feature is particularly useful for preparing lectures, project presentations, or research talks that combine code, results, and narrative in one reproducible workflow.
Although this guide focuses mainly on static two-dimensional plots, which are commonly used in assignments, theses, and academic publications, it is useful to know that RStudio is capable of producing and displaying far more advanced and interactive outputs when your projects require them.
3.2.2.2 Working Directories and Starting your R Projects in RStudio
Once you have installed R and begun working with it, one of the first practical challenges you will encounter is file management (importing data or saving output). Questions such as “Where is my data located?” or “Where did R save my output?” are very common for beginners. This is where the concept of working directories and startup files becomes important.
In simple terms, a working directory is a folder on your computer where R looks for files to read and where it saves files by default. When R is first installed, it assigns a default working directory, which is often your home or “Documents” folder (for example, C:/Users/YourName/Documents on Windows).
Unless you explicitly tell R otherwise, any data you load, plots you save, or files you create will be read from or written to this directory. This is why understanding and controlling your working directory is essential for organized and reproducible work.
While working directories control where R reads and writes files, R also uses a small set of startup and session files to control what is loaded, remembered, or configured when a session begins.
These files do not replace good project organization, but they strongly influence how R behaves when it starts—what objects appear in memory, what commands are remembered, and what settings or environment variables are applied. Understanding them helps explain why R sometimes behaves differently across sessions or directories, especially for beginners.
3.2.2.2.1 Startup and Session Files
As you begin using R, you may notice several files with names that start with a dot (.), such as:
.RData.Rhistory.Rprofile.Renviron
Each of these files can be created manually if needed. In practice, some are generated automatically as you work in R, whereas others are created only when you want to control startup behavior, environment variables, or session settings.
Let’s look at each one in turn.
.RData: Saving and Restoring Your Workspace
The .RData file stores a snapshot of your entire R workspace. This includes all objects currently in memory, such as datasets, model objects, functions, and intermediate results. R creates or updates this file only if you choose to save your workspace when exiting R or RStudio. When you restart R in the same directory, it can automatically reload everything from .RData, restoring your previous session.
While convenient, automatically saving and restoring .RData can cause problems in reproducible workflows:
You may accidentally carry over outdated objects or variables from a previous session.
Scripts that rely on a clean environment may behave inconsistently.
It can make debugging more difficult if old objects interfere with your current analysis.
For these reasons, many experienced users often disable automatic workspace saving. Doing so ensures that each session starts clean, making your analyses more predictable and reproducible.
However, there are valid scenarios where manually saving your workspace as an .RData file is very useful. For example, during computationally intensive workflows, such as training machine learning models with repeated k-fold cross-validation, results can take hours to compute. Re-running the models may also lead to slightly different results due to randomness in resampling or model initialization.
In such cases, saving the workspace as an .RData file allows you to:
Preserve trained models, tuning results, and custom functions
Resume analysis without recomputation
Ensure consistency when preparing tables or figures for reporting
You can manually save your workspace using:
save.image(file = "My_Workspace.RData")And later reload it with:
load("My_Workspace.RData")To improve reproducibility across projects, it is recommended to adjust RStudio’s default behavior:
- Navigate to
Tools→Global Options→ General - Under the Basic tab, uncheck the following:
Restore most recently opened project at startup
Restore previously open source documents at startup
Restore .RData into workspace at startup
- Under Workspace, set
Save workspace to .RData on exitto Never.
Starting each session with a clean workspace minimizes confusion and ensures that projects remain independent and reproducible. These changes aren’t mandatory—you can always adjust them later once you better understand their importance. But implementing them early can save you many headaches as you progress in R.
.Rhistory: Tracking Console Commands
The .Rhistory file stores the history of all the commands you’ve entered in the R console. It allows you to recall past commands easily, making it a helpful tool for navigating through previous work. You can access your command history using the history() function or by simply scrolling through past commands using the up and down arrow keys while in the Console. Remember, in RStudio we can also access our history in the "History" tab under the "Environment | History | Connections | Tutorial" Pane.
That said, relying solely on .Rhistory is not recommended for serious analysis. Important code should always be saved in scripts rather than relying on command history.
.Rprofile: Customizing R at Startup
The .Rprofile file allows users to customize the behavior of R everytime it starts. It is written in R code and can contain commands that automatically run when an R session begins. For example, you might use it to:
Set a preferred working directory
Define custom options (e.g., number formatting, default
stringsAsFactorsbehavior)Load frequently used functions (though beginners are advised to avoid automatically loading packages)
.Rprofile files can exist at two levels:
User-level: Located in your home directory (e.g.,
~/.Rprofile). Settings here apply to all R sessions on your computer.Project-level: Stored in a specific project folder. Settings here only apply to R sessions started in that project.
If both user-level and project-level .Rprofile files exist, R prioritizes the project-level file. You can explicitly include the user-level .Rprofile in a project-level file using:
source("~/.Rprofile")You can create or edit your .Rprofile file conveniently in RStudio using
usethis::edit_r_profile()This will open the .Rprofile file in RStudio for editing.
Tips for Beginners
Keep your .Rprofile minimal at first. Avoid automatically loading packages until you are comfortable with R sessions and reproducibility.
Focus on small customizations that make your workflow smoother, such as setting options or defining simple helper functions.
.Renviron: Managing Environment Variables Securely
The .Renviron file allows you to define environment variables that are automatically loaded when R starts. Environment variables are useful for storing sensitive information, such as API keys, database credentials, or file paths, without hard-coding them into your scripts. This helps keep your workflows secure and reproducible.
The .Renviron file follows a simple key-value format:
API_KEY=abcd1234
DB_PASSWORD=mysecretpasswordYou can access these variables during your R session using Sys.getenv()
Sys.getenv("API_KEY")
# [1] "abcd1234"Similar to the .Rprofile, .Renviron files can also exist at two levels:
- User-level: Located in your home directory (e.g., ~/.Renviron). Variables here are available to all R sessions.
- Project-level: Located in a specific project folder. Variables here only apply when working in that project.
If both user- and project-level files exist, R prioritizes the project-level file, unless you explicitly source the user-level file.
To create or edit your .Renviron file, you can use
usethis::edit_r_environ()This opens the .Renviron file in RStudio for editing, allowing you to add, modify, or remove environment variables.
Tips for Beginners
Avoid storing sensitive credentials directly in scripts. Use
.Renvironinstead.Keep your environment variables organized and documented so you and collaborators can understand them later.
Remember to restart your R session after editing .Renviron to load changes.
| File | Purpose | Scope | Example Use |
|---|---|---|---|
.Rprofile |
Run R code automatically at startup | User or project | Set global options, define helper functions, configure project-specific behavior |
.Rhistory |
Store console command history | Per directory/session | Recall and reuse previously run commands |
.Renviron |
Store environment variables securely | User or project | API keys, authentication tokens, library paths |
.RData |
Save and restore the entire R workspace | Per directory | Preserve trained models, analysis objects, or long-running results |
Understanding these files early in your R journey helps you avoid confusion and develop good programming habits. Not every project will require all of them, and many beginners can work comfortably for a long time without touching most of these files.
However, knowing what each file does, when it is created, and how it affects your R sessions gives you greater control over your workflow. More importantly, it prepares you to understand why tools like RStudio Projects exist and how they solve many of the problems caused by unmanaged working directories and hidden session state.
3.2.2.2.2 How Working Directories Fit Into the Bigger Picture
Now that we understand how R manages startup behavior and session state, we return to a concept that beginners interact with constantly—the working directory. Unlike startup files, which affect how R starts, the working directory determines where R reads data from and where it writes output during your analysis as previously described.
In RStudio, the current working directory is shown at the top of the Console pane, making it easy to confirm at a glance. You can also check your current working directory by running the command getwd() in the console.
getwd()This command prints the full file path of the directory R is currently using.
If you’re not using RStudio Projects, you must manually set the working directory at the start of every R script using the setwd() function:
setwd("C:/Users/Elias Nelson Kaunda/Documents/Intro-to-R")This tells R to use the “Intro-to-R” folder as it’s working directory. From that point onward, any files you load or save will be read from or written to this folder by default.
For example, when you load a dataset using read.csv("data.csv"), R will look for data.csv inside the "Intro-to-R" directory.
However, there’s a catch; setwd() uses an absolute file path, which is specific to your computer’s directory structure. If you share your script with someone else or switch to a different computer, the path will fail unless their directory structure matches yours exactly. In practice, this almost never happens (you are unlikely to have a directory structure like mine: "C:/Users/Elias Nelson Kaunda/Documents/Intro-to-R" on your computer).
This creates problems for:
Collaboration
Long-term projects
Reproducibility
Moving between computers
Because of this, relying on setwd() is generally discouraged for serious or shared work.
RStudio provides a solution to this problem through R Projects (files with the .Rproj extension). Using RStudio projects eliminates so much of the early-stage hassle and confusion around reading in and exporting data. When you open an R Project, RStudio automatically sets the working directory to the folder containing the project file. This removes the need to manually set the working directory and allows you to use relative paths throughout your analysis.
Relative paths are paths that start from the project root rather than from your computers file system. This makes scripts portable, reproducible, and easy to share while collaborating with others.
For example, if you need to share your analysis with a colleague, you can send the entire project folder. Once your colleague opens the .Rproj file in RStudio, all scripts that rely on relative paths will work as expected, without needing to manually set or modify the working directory.
Using R Projects offers several key benefits:
File Organization: All files related to a project—data, scripts, figures, and reports—live in one dedicated folder. This keeps your work tidy and easier to understand.
Reproducibility: Relative paths ensure that analyses run the same way on different computers without modifying file paths.
Collaboration: You can share the entire project folder with a colleague, who can open the .Rproj file and run your scripts immediately.
Version Control: R Projects integrate seamlessly with Git and GitHub, allowing you to use version control to track changes to your code over time. This is especially useful for collaboration or when you need to roll back to a previous version of your analysis.
Convenience: RStudio automatically manages the working directory for you, reducing setup errors and mental overhead.
3.2.2.2.3 Creating a New R Project in RStudio
Here’s how you can create a new R Project in RStudio:
- Navigate to File → New Project from the menu bar
or use the project dropdown in the top-right corner and select New Project.
- In the dialog box, choose New Directory.
- Next, select New Project.
- Enter a clear, descriptive name for your project folder (referred to as the Directory Name) that reflects the nature of your work. Make sure to save this project folder in a logical and organized location on your computer. You can adjust the folder’s location by clicking Browse… and selecting the appropriate directory. Ideally, this should be a folder where you routinely store your research or analysis projects, ensuring easy access and consistency in file management.
Optionally, you can create a Git repository for version control by ticking the Create a git repository checkbox. If you’re unfamiliar with Git or don’t use GitHub, you can safely ignore this option for now.
Check the Open in new session option to open your new project in a fresh RStudio window.
Once you’re satisfied with the settings, click Create Project. RStudio will open a new session for your project, and you’re ready to begin.
Once your project is set up, you’ll notice a few changes in RStudio:
The project name will appear at the top of the RStudio window.
The Files pane will show the root directory (i.e., your project folder), making it easy to navigate through your project files.
The Console will display the current file path of your project.
This setup ensures that all your scripts, data, and output are stored together in one place. While you could configure all this manually using setwd(), R Projects make the process much more intuitive and efficient.
3.2.2.3 Organizing Your Projects and Structuring Your Working Directories
This section is not about the technical aspects of RStudio, R, or data analysis per se. Instead, it’s about the often overlooked yet crucial habit of organizing your work through a solid folder structure. Like good coding practices, setting up a clear, logical folder structure can significantly boost your productivity and reduce frustration. A well-structured project directory will help you quickly locate files, avoid clutter, and make your work easier to share or revisit later. This is especially important when working with R projects in RStudio.
Before you start coding, take some time to plan your folder structure. Having dedicated folders for different file types, rather than dumping everything into one container, will save you time and prevent headaches down the line. Whether you create your folder structure within RStudio or through your file browser, the key is to be organized.
Good folder organization also plays a critical role in reproducible research and analysis. Proper directory setup is one of the “non-code” aspects of R programming that significantly enhances your workflow, and for beginners, it should be a priority. Structuring your directories in a way that’s easy to navigate ensures that collaborators—or your future self—can follow and reproduce your work effortlessly.
A Basic Folder Structure:
Aside from using RStudio projects, it’s also good practice to set up a working directory that makes your project easy to navigate. Here’s a basic template you can use as a starting point:
For each project I work on, I usually have at least the following folder structure:
Data: This folder contains all the files you’ll need to import into R for analysis or visualization. These could include CSV, Excel, SPSS (
*.sav),.FST,.RDS,.tif,.shp, or.gpkgfiles, depending on your work. It’s a good idea to create two separate subfolders: one for raw data (untouched, as collected or downloaded) and another for processed or cleaned data (after some wrangling and transformation in R). This ensures that you keep your original data intact while also organizing the processed data outputs.R Scripts: Here, you save all your R scripts. This includes your analysis code as well as any custom functions you may write. You might consider creating a subfolder specifically for your functions if you find it convenient to keep them separate. Saving your scripts and functions this way allows for easy re-use across different analyses and projects. Rather than creating separate projects for every analysis, you can store multiple scripts in one project folder—making it easier to manage different tasks on the same dataset.
Output: Use this folder to store all the outputs generated from your scripts, such as plots, reports (e.g., HTML or PDF), and any data exports. This folder helps differentiate between input data and the final results, ensuring others (or you, in the future) can easily identify the outputs of your analysis.
In RStudio, your directory structure might look like this:
This template should serve as a good starting point if you’re new to project organization. However, remember that each project has its unique requirements. You should always adapt the directory structure to fit the specific needs of the project while maintaining a level of consistency that simplifies collaboration and future work.
It’s also a good practice to use lowercase for folder names, as shown in the example above. This makes it easier when you reference folder names in your code for reading or writing files, reducing potential errors related to case sensitivity.
By adopting this structure early on, you’ll streamline your workflow, improve the reproducibility of your work, and make it easier to share or revisit your projects with confidence.
3.2.2.4 Writing Scripts in RStudio
Code quickly becomes long and complex. Thus, it is not very convenient to write it in the console. So, instead, we can write code into an R Script. Remember the “R Editor” window we encountered in the base RGUI—well the R Script in RStudio could be considered as it’s equivalent, but much better. An R Script is a document that RStudio recognizes as R programming code. Files that are not R Scripts, like .txt, .rtf or .md, can also be opened in RStudio, but any code written in it will not be automatically recognized.
When opening an R script or creating a new one, it will display in the “Source“ window. Some refer to this window as the ‘script editor’. An R script starts as an empty file. Good coding etiquette, demands that we use the first line to indicate what this file does by using a comment ‘#’.
Perhaps one of the most important aspects of making your code comprehensible for others and your future self is adding comments about why you did something. You can write comments directly in your script, and tell R not no execute those words simply by putting a hashtag (#) before you start typing the comment.
Comments can be used to explain R code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Comments starts with a #. When executing code, R will ignore anything that starts with #.🔥
Below are examples of how you can use comments in R’s syntax:
# Load dplyr package
library(dplyr)
mtcars_summary <- mtcars %>%
filter(cyl == 6) %>%
select(mpg, hp, wt) %>%
summarise(
avg_mpg = mean(mpg),
avg_hp = mean(hp),
avg_wt = mean(wt)
)- 1
-
dplyris one of the cornerstone of the tidyverse, really useful for data wrangling. - 2
-
here I’m computing 3 variables, the averages of
mpg,hpandwt
- Hover over the
circled numbersfor additional context.
Here is an example.
# Create a boxplot to visualize miles per gallon (mpg) by the number of cylinders (cyl)
# Load the built-in 'mtcars' dataset
# This dataset provides information on various car models and their specifications
head(mtcars) # Display the first few rows to understand the structure of the dataset mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# Create a boxplot using base R functions
# Group the 'mpg' variable by the 'cyl' variable (number of cylinders)
boxplot(mpg ~ cyl, # Formula notation indicating mpg by cyl
data = mtcars, # Specify the dataset
main = "Miles per Gallon by Number of Cylinders", # Title of the plot
xlab = "Number of Cylinders", # Label for the x-axis
ylab = "Miles per Gallon", # Label for the y-axis
col = c("#FF5733", "#6A0DAD", "#3BB143"), # Colors for each box
border = "grey20") # Border color of the boxes# Comments like these help explain what each part of the code does and why it is used.
# Always include comments in your R scripts for better readability and understanding.All examples in this post can easily be copied and pasted into your own R script. Let’s try it with the following code. The plot this code creates reveals which car manufacturer produces the most efficient cars. Copy and paste this code into your R script.
3.2.2.5 Updating and RStudio: Staying on the Cutting Edge
While regularly updating your software might not directly improve your programming skills, it can certainly save you from unnecessary frustration down the road. It’s important to remember that R and RStudio are separate programs, meaning you need to update them individually. Although they work closely together, updating RStudio won’t automatically update R itself, and vice versa.
This distinction becomes crucial when certain tools or packages you’ve installed through RStudio require the latest version of R. For instance, if you’re using a cutting-edge machine learning algorithm that’s incompatible with older versions of R, you might run into errors or performance issues. Likewise, R packages developed by the community (which we’ll explore later) also need to be updated independently—they are not automatically refreshed when you update R or RStudio.
Now, I know this sounds a bit tedious, but don’t worry—it’s simpler than it seems. Updating packages within RStudio is straightforward, and I’ll guide you through it. Just keep in mind: R, RStudio, and your packages all need to be updated separately.
By staying on top of updates, you’ll ensure that your coding environment remains compatible, efficient, and equipped with the latest features, sparing you the hassle of unexpected errors or incompatibility down the line.
3.2.3 Visual Studio Code: A General-Purpose IDE and Code Editor for Software Developers and Programming
RStudio is often the first choice for running code, largely because it was designed specifically for the ecosystem and provides a very smooth out-of-the-box experience. However, Visual Studio Code, commonly referred to as VS Code has quickly gained traction as a versatile code editor among software developers working across many programming languages.
Speaking from personal experience—and perhaps echoing the sentiment of others in the and broader programming communities—using in VS Code often starts as an experiment to find an all-in-one code editor. The motivation is simple: the desire to work within a single editor for all programming tasks. If you already use VS Code for Python, HTML, CSS, JavaScript, or other languages, it feels natural to ask whether can also fit into that same workflow.
VS Code is particularly well suited for projects that involve multiple technologies. For example, data analysis in R combined with Python scripts, documentation written in Markdown, and web components written in HTML or JavaScript can all live comfortably in one place. This reduces the constant switching between tools and helps maintain focus when working on complex projects.
That said, VS Code is not an -specific IDE. It is a general-purpose source code editor with a very large extension ecosystem. This flexibility is one of its greatest strengths, but it also means that a smooth experience requires some initial setup.
Visual Studio Code supports the programming language through extensions. The most important of these is the extension for Visual Studio Code, which connects VS Code to your existing installation.
Once properly configured, VS Code can provide many features users expect, including:
Syntax highlighting for scripts
Code completion and function suggestions
Inline diagnostics and error messages
Code linting and formatting
Interaction with an terminal
Viewing data frames, plots, workspace variables, help pages and more
While these features may feel familiar to RStudio users, it is important to note that in VS Code they are powered by a combination of extensions and packages working together.
Key idea:
RStudio ships with -specific tools built in.
VS Code adds features through extensions and packages.
Setting up VS Code for isn’t as straightforward as installing RStudio. This is not a limitation, but rather a design choice. VS Code is designed to support dozens of programming languages. If it shipped with full support for all of them by default, it would be overly complex and resource-heavy. Instead, VS Code uses a modular approach where users install only the extensions they need.
RStudio, on the other hand, is iconically tailored for . Its tight integration means that many features work immediately after installation without any additional configuration.
The flexibility of VS Code is powerful, especially for developers working across multiple languages, but it does mean that configuring VS Code for requires a few extra steps.
To run in VS Code, as with RStudio, you must already have installed on your machine.
version
3.4.0or later is requiredInstalling using a precompiled binary is recommended for most users
Building from source is possible but not necessary for beginners
Since we have already covered how to install on Windows and macOS earlier in this guide, we will skip those steps here and focus instead on configuring VS Code to work with .
3.2.3.1 Setting Up Packages for VS Code Integration
To ensure a smooth experience using in VS Code, a few essential packages must be installed before setting up the editor itself.
An important question would be “What Are
Packages”
If this is your first encounter with packages, do not worry. We will explore them in much more detail later in this guide. For now, a simple analogy helps.
Think of as a new smartphone. When you first buy it, it comes with basic apps and settings that allow it to function. Over time, you may want to extend its capabilities by installing additional apps from the Google Play Store or Apple App Store.
packages work in much the same way. packages are add-ons that give you extra tools—whether you’re cleaning data, creating charts, or building statistical models. Base comes with many built-in functions and packages that handle common tasks. However, as your work becomes more specialized, you will need additional tools. packages provide these tools in the form of reusable functions, datasets, and documentation.
Each package is a small, self-contained toolkit. The more packages you install, the broader the range of tasks you can perform. Packages are also a cornerstone of reproducible research, since they allow others to run the same code using the same tools.
Most packages are distributed through the Comprehensive Archive Network (CRAN), the official public repository for packages. Although packages can also be installed from local files or other online sources, you do not need to manually visit the CRAN website to download them. Instead, packages can be installed directly from the console using the install.packages() function.
Not every package is hosted on CRAN. Some are available from alternative platforms such as GitHub or Bioconductor, which provide additional or specialized tools.
To enable support in VS Code, you will need to install at least one compulsory package from CRAN. The key package required fr integration is languageserver.
Open and run the following command in the Console:
# Language Server support
install.packages("languageserver") - For VS Code,
languageserveris required. Everything else is optional (simply recommended).
This languageserver package allows VS Code to understand your code. It provides important development features such as code completion, inline diagnostics, function documentation, and object inspection.
While not strictly required, you may also choose to install a few more optional packages that improve the overall programming experience in VS Code. These packages are not required for to function in VS Code, but they provide useful quality-of-life features when using in VS Code.
Two commonly recommended optional packages are lintr and httpgd. Just as languageserver, the lintr packae is also available on CRAN and can be installed with the following command:
# Code linting support
install.packages("lintr")The lintr package checks your code for potential issues related to style inconsistencies, potential syntax problems, and violations of recommended best practices. When integrated with VS Code, these checks appear as you type, helping you build good coding habits early on.
When you run install.packages() for the first time, may ask you to select a CRAN mirror.
CRAN, short for the Comprehensive R Archive Network, is a global network of servers that store packages. A CRAN mirror is simply a geographically hosted copy of this repository in different locations around the world.
- A
CRANmirror is simply a copy of the same packages hosted in different countries.
You can safely choose any mirror. If you prefer, you may manually select a mirror located in a country close to you, which can sometimes improve download speed.
If you are unsure, selecting 0-Cloud [https] at https://cloud.r-project.org/ is usually the best option. This is not a single server, but a content delivery network that automatically connects you to a nearby server and is generally the best default.
Most importantly, all CRAN mirrors contain the same packages. The choice only affects download speed, not package content.
Many packages rely on other packages to function properly. These supporting packages are called dependencies.
When installing a package, automatically installs any required dependencies. Seeing multiple packages install at once is normal and expected, so do not be alarmed if you see several additional packages being installed.
After selecting a CRAN mirror, during the installation process, you may see the prompt:
“Do you want to install from sources the packages which need compilation?”
This question relates to the two ways packages are distributed and can be confusing for beginners, so let us break it down.
packages are distributed in two main forms:
Binary packages (recommended for beginners)
Already compiled for your operating system
install quickly
Do not require additional tools
Source packages
Contain original source code
Must be compiled on your computer
Require developer tools (compilers):
Windows:
RtoolsmacOS:
XcodeCommand Line ToolsLinux: system build tools
When asks whether you want to install from source, it is essentially asking whether you want to compile the package yourself or use a ready-made version.
For beginners, the recommended answer is usually NO, because:
Binary packages are easier to install
No additional developer tools/software is required
There is a lower chance of installation errors
When you select NO, installs pre-compiled binary versions automatically and skips source compilation.
Choosing Yes only makes sense if:
You understand what package compilation is
You already have the required tools installed (e.g., Rtools)
You are following advanced documentation that explicitly tells you to install from source.
For most beginners, this is not necessary.
On Windows and macOS, usually just installs pre-compiled binary packages automatically, so unless tools like Rtools or Xcode are installed, this prompt may not appear at all. Do not be concerned if you never see it.
- Beginner rule of thumb 🧠 If unsure, always choose
NOwhen asked about source installation.
Once you click NO, will start installing the packages until you should see a message indicating that the downloaded binary packages have been installed successfully.
Another useful optional package is httpgd, which provides a modern interactive graphics device that works well inside editors like VS Code.
Normally, installation from CRAN would be:
# Install httpgd:
install.packages("httpgd")httpgdis an asynchronous HTTP server graphics device for that enables live graphics to be viewed inside web browsers and IDEs
If this command succeeds, no further action is required.
However, installation may sometimes fail with a warning similar to the one shown below:
When a package installation fails, it usually indicates one of the following:
The package is temporarily unavailable as a binary on the main CRAN repository.
The package has been archived on CRAN.
Your system must compile the package from source
Archived packages remain available in CRAN’s archive directory, but they are no longer installed through the standard install.packages() workflow.
Common reasons a package becomes archived include:
The package is no longer maintained by its author.
It fails CRAN quality checks on newer versions or operating systems.
Licensing or CRAN policy issues arise.
The maintainer has orphaned the package (stopped maintaining it).
In addition, archived packages may depend on older versions of {{fa brands r-project}} or other packages, which can make installation more complex.
This particular situation introduces the need for build tools
Why Build Tools Become Necessary Many packages often contain compiled code written in C, C++, or FORTRAN. To install such packages from source, your operating system must provide a compiler toolchain, commonly referred to as build tools.
🖥️ Windows: Installing Rtools
Windows users need to install Rtools following the steps as below:
Firstly check your version.
In , run:
R.version.stringThis returns a version string such as:
"R version 4.5.2 (2025-10-31)"Make note of the version.
Download the matching
RtoolsversionVisit the CRAN
Rtoolspage: https://cran.r-project.org/bin/windows/Rtools/Choose the installer that corresponds to your installed version.
Under
installing Rtools4x, click theRtools4x installerlink to begin downloading the installer.
Run the installer
Accept defaults unless you need a custom installation path.
Keep all the Additional Tasks options selected. The default configuration is recommended and requires no changes for a standard Rtools installation.
Click install.
The installation process will proceed as below:
Verify installationOpen/restart and run.
system("make --version")If you see
Makeversion info, Rtools is correctly installed.
🍎 macOS: Installing Xcode Command Line Tools
Open Terminal
- Press
Cmd/⌘ + Space, type Terminal, and hitEnter↵.
- Press
Install the tools
To install the tools Run:
xcode-select --install
Confirm installation
After installation, verify with:
gcc --versionIf you see version info, the tools are installed.
🐧 Linux: Installing Development Tools
Update your package manager
Ubuntu or Debian:
sudo apt-get updateFedora or RedHat:
sudo dnf update
Install build tools
Ubuntu/Debian:
sudo apt-get install build-essential r-base-devFedora/RedHat:
sudo dnf groupinstall "Development Tools" sudo dnf install R-devel
Verify installation
Verify installation using:
gcc --version
If you see version info, the compiler is ready.
Once the build tools are successfully installed, you can proceed with installing httpgd, which enables an interactive graphics device for R in VS Code.
Begin by installing the remotes package, which allows installation of packages directly from GitHub:
install.packages("remotes")Before installing httpgd, it is recommended to pre-install unigd, a required dependency of:
# Install unigd from GitHub
remotes::install_github("nx10/unigd")You can then install httpgd from GitHub:
# Install httpgd from GitHub
remotes::install_github("nx10/httpgd")Compilation may take a few moments. A successful installation will with the message DONE (httpgd) in the comsole.
Together, these packages provide a solid and beginner-friendly foundation for working with in VS Code.
In the next section, we will shift focus to Visual Studio Code itself, where we will install the necessary VS Code extensions and configure the editor for everyday development.
3.2.3.2 Installing and integrating VS Code with
The first step in using inside Visual Studio Code is installing the editor itself. VS Code is free, lightweight, and available for all major operating systems.
Step 1: Download Visual Studio Code
Begin by downloading Visual Studio Code from the official website: 👉 https://code.visualstudio.com/Download
Choose the installer that matches your operating system (Windows, Mac or Linux).
Step 2: Start the Installation
VS Code intsalls like any other desktop application.
On Windows, double-click the downloaded
.exefile to start the installer.On macOS, open the downloaded archive and drag VS Code into your Applications folder.
On Linux, follow the instructions provided for your distribution.
Step 3: Accept the License Agreement
Read through the license agreement, accept the terms, and click Next to continue.
Step 4: Choose the Installation Location
Unless you have a specific reason to install VS Code elsewhere, it is best to keep the default installation location.
Click
Nextto continue.Step 5: Start Menu Folder
The default
Start Menu Folderworks well for most users. Leave this unchanged and clickNext.Step 6: Select Additional Tasks (Recommended Settings)
This step is easy to rush through, but it is worth paying attention here.
Here is what these options do and why they are useful:
Create a desktop icon:Optional. This simply places a VS Code shortcut on your desktop for quick access.
Add “Open with Code” to file context menu:This allows you to right-click a file in Windows Explorer and open it directly in VS Code. Very convenient when working with R scripts, Quarto files, or project folders.
Add “Open with Code” to directory context menu:This lets you right-click an entire folder and open it as a workspace in VS Code. This is especially useful for R projects, as VS Code will treat the folder as a single working environment.
For beginners, it is safe and recommended to check all three options. They do not change how VS Code behaves internally; they simply make it easier to open files and projects.
After selecting your preferred options, click
Next.Step 7: Install VS Code
Click Install to begin the installation process.
You will see a progress screen while VS Code is being installed.
Step 8: Finish and Launch
Once installation is complete, click
Finish.VS Code should launch automatically.
Step 9: First Launch
When VS Code opens for the first time, you will see a welcome screen. This includes links to tutorials and options to customize the editor’s appearance.
For now, you can leave everything as-is and simply close the welcome tab.
At this point, Visual Studio Code is successfully installed on your system. Next, we will install the extension extension for VS Code, which allows the editor to understand and run code.
This extension adds -specific features such as:
Syntax highlighting
Code execution
An -aware terminal
Basic editor support for scripts
Open the Extensions view:
Inside VS Code, look to the Activity Bar on the left-hand side. You will see icons for:
Explorer
Search
Source Control
Run and Debug
Extensions.
Click on
Extensions(the four-squares icon), or use the keyboard shortcut:Windows/Linux:
Ctrl + Shift + XmacOS:
⌘ + Shift + X
Install the extension:
In the extensionss search bar type
REditorSupport.r.Select the extension
Click
install
Once installed, VS Code now understands code.
Confirm the extension is active
After installation, you should notice a new icon appear in the Activity Bar, usally just below the Extensions icon. This confirms that the is active and ready to use
3.2.3.3 Making discoverable by VS Code: Adding to PATH
For VS Code (and your system terminal) to run , it needs to know where the executable lives. This is handled through a system setting called PATH.
Think of
PATHas a list of folders your computer searches when you type a command like into a terminal.
On Windows
Locate your installation
By default, is installed in a folder similar to:
C:\Program Files\R\R-x.x.x\binCopy the path
Open File Explorer, navigate to the
binfolder inside your installation. Right-click the folder and selectCopy as path. Alternatively you can use the keyboard shortcutCtrl/⌘ + Shift + COpen Environment Variables.
Press
Windows key + R, typesysdm.cpland hitEnter↵. Go toAdvancedtab and clickEnvironment Variables.
Edit the PATH variable.
Under System variables, select Path, click Edit → New, paste the path you copied (C:\Program Files\R\R-x.x.x\bin). Click OK to save.
Once this is done, typing in a terminal should work anywhere on your system.
To show a quick demonstration of what this means. Open Command Prompt to run R in the terminal you can simply run:
ROr if you would prefer the vanilla console run:
R --vanillaThe vanilla console refers to a basic session that does not load any custom settings ( startup file configurations e.g., .Rprofile) or libraries.
Running this command opens in terminal and you can run code in the terminal.
3.2.3.4 Pointing VS Code directly to (Rpath setting)
Sometimes the extension needs an explicit pointer to the executable.
To set this:
Go to
File → Preferences → SettingsSearch for
RpathSet the value to the full path of your executable:
Windows:
C:\Program Files\R\R-x.x.x\bin\R.exemacOS/Linux:
/usr/local/bin/R
This removes any ambiguity and ensures VS Code always uses the correct installation.
3.2.3.5 Starting an session inside VS Code
- Open a terminal:
Menu:
Terminal→New Terminal.Shortcut:
Windows/Linux:
Ctrl + `macOS:
⌘ + `
Click the dropdown arrow next to the
+button in the and selectCommand Prompt.In the terminal type:
Rand press
Enter↵to start ansession.Alternatively, from the same dropdown menu next to the
+button, selectR Terminal. This launches an session automatically and is often the easiest option for beginners.Here you can run code as we did before in the base Console or in RStudio’s Console pane. You can simply run a simple
print("Hello, World")program as we did previously. Go ahead and also try a simple arithmetic computation after that.
Up to this point, everything we’ve done has been purely about .
Now we introduce radian, an enhanced console that provides:
Syntax highlighting
Better auto-completion
A smoother interactive experience inside VS Code
Radian is an alternative console for . To integrate radian with VS Code, we need to have and
I get it, right now you’re wondering how python has made it into our discussion, but take heart and don’t be too overwhelmed.
Here’s the key point:
Radian is written in Python.
That’s why Python is required — not because you need to learn Python, but because it acts as a supporting tool for a better experience.
So think of Python here as infrastructure, not a new language you must master.
However a very short introduction to Python may be imminent as we need to install the program for us to continue and integrate radian with VS Code. Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. It is widely known for its readable syntax and is commonly used as a foundation for tools, automation, and scientific software.
In our case:
Python is required to install
radianWe only need Python and
pip(Python’s package manager)No Python programming knowledge is required
You can safely continue thinking “in throughout this section.
Before installing Python you should first check if python isn’t already installed on your computer:
On Windows, to open Command Prompt
Press
Windows key + RType
cmdin theRun Dialog BoxPress
Enter↵or clickOKIn the Command Prompt terminal, type:
python --versionthen press
Enter↵.If Python is installed, you’ll see a version number e.g.,
Python 3.12.7.If not, Windows will report that Python is not recognized.
Tip: Use your mouse to copy and paste commands — standard Ctrl/⌘ + C and Ctrl/⌘+ V keyboard shortcuts may not work in some terminals.
On macOS
Press
⌘ + SpaceSearch for Terminal and press
Enter↵.Run the same command:
python --version
If Python is not installed follow the steps below to install it:
Visit www.python.org
Click
DownloadChoose the installer for your operating system
For Windows users click the
Windowslink and then click on theDownload Windows installer (xx-bit)for your appropriate bit type under theStable Releasesunder Stable ReleasesFor other users (macOS or Linux) follow the links provided to download python.
Install Python
For Windows users simply double-click the
.exeinstaller to begin the installation process.Before clicking Install Now, check the following options:
- ☑︎ Use admin privileges when installing py.exe
Ensures that the installation proceeds without restrictions as the installation process requires elevated permissions to modify system files and settings,
- ☑ Add python.exe to
PATH
This is important because you’re tellig your operating system where to find python (or python3). This allows you to run Python from the terminal by typing python. Remember we earlier had to do this manually for , well this affords to avoid the gruesome some of having to do that manually for Python.
If you already have another Python version installed, PATH order matters. In such cases, tools may use the first Python version found. Advanced users often use version managers, but beginners can safely proceed with the default setup and explicitly call the version you want e.g., python3.11 or python3.12 when the need arises
Click
Install Nowand let the installation complete.Click close once the installation is successful as below.
You are now fully prepared to install and configure radian, giving you a powerful and pleasant interactive console inside VS Code — without needing to learn Python itself.
Finally to install radian :
Open the
Command Promptterminal while in VS Code as we did before.Firstly make sure you are using an updated version pip. the safest is to upgrade pip, setuptools and wheel. Outdated packaging tools often can cause errors. In your opened command prompt terminal run:
python -m pip install --upgrade pip setuptools wheelAfter making sure you have updated packing tools, run the following command to install
radian:pip install radianThe installation process should take a whille but a successful installation of
radianwill look like:To further confirm
radiansinstallation run the command below in the same terminal:radian --versionIf everything was successful, this will print the version of radian installed, including the paths to and python executables and their corresponding versions installed. This is where you may also notice that the manual addition of to
PATHand python’s automatic addition during installment saves us from future headaches and errors that we my have faced such as here. Radian is just a front-end; it needs to run, hence it has to be added toPATH.Many people prefer using the
radianas an alternative Console for . You can configure VS Code to use radian by setting the path to theradianexecutable in yoursetting.json. To setradianas your default terminal in VS Code settingsFor windows the radian executable should be located in your Python
Scriptsfolder (i.e.,"C:\Users\ENTOMOLOGY LAB\AppData\Local\Programs\Python\Python314\Scripts\radian.exe"). While on a n a Mac, this is typically"/usr/local/bin/radian", while for Linux"/usr/bin/radian".You can access settings.json by pressing
⌘ + Shift + Pon Mac orCtrl + Shift + Pon Windows/Linux, typing“Open Settings (JSON)”, and pressingEnter↵.Configure VS Code settings
- Open VS Code
- Press Ctrl + space,
- Search for
settings.json.
For windows add the following line:
{ "r.term.windows": "C:\\Users\\ENTOMOLOGY LAB\\AppData\\Local\\Programs\\Python\\Python314\\Scripts\\radian.exe" }
For Mac:
{
"r.term.mac": "/usr/local/bin/radian"
}For Linux
{
"r.term.linux": "/usr/bin/radian"
}The radian terminal provides syntax highlighting, multiline editing, and autocompletion in the console. Standard terminal lacks these features. Radian makes the interactive much more usable with modern terminal features.
There are a few more recommended VS Code settings for . These settings help to optimize your workflow when working with in VS Code. You can add these settings in your settings.json file as we did with the radian executable:
{
"r.bracketedPaste": true,
"r.sessionWatcher": true,
"r.alwaysUseActiveTerminal": true,
"r.removeLeadingComments": true,
"r.plot.useHttpgd": true,
"editor.wordWrap": "wordWrapColumn",
"[r]": {
"editor.defaultFormatter": "REditorSupport.r"
},
"workbench.colorCustomizations": {
// "editorError.foreground": "#00000000", // Red Squiggly, I suggest not to change this one…
// "editorWarning.foreground": "#00000000", // Yellow Squiggly, I suggest not to change this one…
"editorInfo.foreground": "#00000000", // Blue Squiggly, you could even change it yo something almost completely transparent so it doesn’t bother you as much…
}
}An integral part of setting up VS Code for R development is fine-tuning the behavior of the lintr package, which plays a crucial role in maintaining high code quality and adherence to coding standards. By customizing the .lintr configuration, developers can control which linting rules are enforced, tailoring the development environment to their preferences or project-specific guidelines.
Enhance your R programming experience in VS Code by tweaking some settings in your settings.json file.
Migrating from RStudio to VS Code for R development not only opens up new possibilities for enhancing your development environment but also requires some adjustments to tailor the experience to your preferences.
By following the above steps, you’ve now equipped VS Code with powerful tools and configurations to supercharge your programming. With the extension, radian, essential packages, and tailored settings, you’re all set to enjoy a streamlined, productive coding experience in . Whether you’re analyzing data, creating visualizations, or developing statistical models, VS Code is now a formidable ally in your programming endeavors. Happy coding!
This is one of the little maintenance chores that comes with upgrading on Windows is you need to adjust paths.
3.2.4
— A New, Yet Familiar IDE for and Python
Positron is a modern, next-generation Integrated Development Environment (IDE) by Posit with data scientists and analysts at its core. It is designed to act as a single, unified workspace for coding, data exploration, visualization, and analysis.
At its heart, Positron brings together more than a decade of lessons learned from RStudio, while embracing the flexibility and extensibility of Visual Studio Code. Technically, Positron is built as a fork of Visual Studio Code (
Unlike traditional editors that focus on one language at a time, Positron is polyglot by design. Both and
Under the hood, Positron is built on Code - OSS, the same open-source foundation that powers VS Code. This gives it access to a rich ecosystem of extensions and tools, while Positron layers on features long familiar RStudio users — such as an integrated console, data viewer, and workflow-oriented panels — now available to both and
You can think of Positron as a thoughtful blend of two worlds: the extensibility of VS Code and the data-focused ergonomics of RStudio. If those two IDEs were ever to “join forces,” Positron would be the natural result.
Positron should not be confused with RStudio, the classic IDE many users already know. In 2022, RStudio, PBC rebranded as Posit to reflect its broader focus beyond R alone. Positron emerged from this evolution as a new IDE built to support both and
Positron is guided by a few key ideas which form its core design principles:
Polyglot - Work with ,
Python, and other languages in a single, cohesive environment. Extensible - Customize and extend Positron using VS Code-compatible extensions available through the Open VSX marketplace.
AI used responsibly - Leverage AI-assisted tools while maintaining transparency, correctness, and reproducibility in your work.
Positron was introduced as public beta in 2023 and evolved through several iterative releases between 2023 and 2025. During this period, it matured into a stable, cross-platform IDE for data science. Version 2026.01.0-147 marks the first stable release, distributed under the Elastic License 2.0. This makes Positron source-available — you can inspect the code — but it is not fully open-source in the traditional sense.
3.2.4.1 Hello Positron IDE: Installing 
Positron IDE is available on all major platforms, including Windows, macOS, and Linux. The steps below walk through installing the stable desktop version, suitable for individual users.
Download Positron
Visit the official Positron website
Click
DownloadChoose the right Positron edition
On the download page, you’ll see two options:
Positron Desktop— a free, standalone IDE installed directly on your computer.Positron Pro— an enterprise-grade version integrated with Posit Workbench, designed for teams that need centralized security, governance, and access to shared computing resources
If you are learning or working independently, Positron Desktop is the correct choice and is what we’ll use here.
Windows Prerequisites: Visual C++ Redistributable
If you are installing Positron on Windows, one additional requirement is important to check that you have the latest Visual C++ Redistributable installed.
Visual C++ Redistributable is a Microsoft runtime library that contains essential components needed to run applications built with Visual Studio C++. Positron is partly built using Visual C++ components and without the redistributable, cthe IDE may fail to install or launch, sometimes showing errors such as:
MSVCP140.dll missingruntime error.
To check if the latest Visual C++ is installed:
Press
Windows Key + R to open the Run dialog. Type
appwiz.cpland pressEnter↵— this opensPrograms and Features.Look for entries such as:
Microsoft Visual C++ 2015-2022 Redistributable (×64)Microsoft Visual C++ 2015-2022 Redistributable (×86)
If you see any of these, it means the redistribtable is installed on your platform. The (×64) version is the most common on morden systems.
If not installed, download the latest version from 👉 link.
One the page navigate to the table with links to the apporiate section. click on the latest versions link.
You need to download the appropriate Visual Studio C++ for your systems architecture. To check Your Windows Architecture
Press
Windows Key + I to open Settings.Go to
System About.Look for
System type.
If it says
64-bit operating system, ×64-based processor, you need the×64redistributable.If it says
64-bit operating system, ARM-based processor, you need theARM64redistributable.(Rarely, if it says
32-bit operating system, you’d need the×86redistributable, but most modern systems are×64orARM64.)
Click on the appropriate installer link for your architecture. After the ownload is complete double click the installer and install it like any application. You will have to restart your computer was the installation is complete
Download the Positron installer
Once prerequisites are met, return to the Positron download page, check the license agreement box, and download the download the installer for your platform.
On Windows, you’ll be offered two installer types: the
system level installanduser level installinstaller options. If you’re the only person using the computer and you have admin rightsSystem-levelis simpler and more standard. If you’re on a work/school computer without admin rights, or you want to keep things isolatedUser-levelis the safer choice. Both versions of Positron are functionally the same — it’s just about installation scope and permissions.
4 Programming Basics and Fundamentals
After a likely tedious installation of and an IDE such as RStudio, VS Code or Positron, as well as a detailed familiarization with their interfaces, We are now ready to do a little more coding and explore the fundamentals of programming.
In this section, we introduce the core building blocks of programming. These fundamentals form the foundation for everything you will do in , from simple calculations to more advanced analyses. Developing a strong understanding of these basics early on will make your earning experience smoother and more intuitive as you progress to topics such as data manipulation, visualization, and statistical modeling.
It can be tempting to skim or skip over these early technical details, especially if they feel abstract at first. However, these concepts quietly underpin much of what comes later in . Taking the time to understand them now will save you confusion and frustration down the line, when small misunderstandings can lead to puzzling errors in more complex workflows.
4.1 Commands, Expressions, Assignments, Operators, and Objects
At this point, you have already written your first "Hello, World!" program and performed some basic arithmetic in , much like using a handheld calculator. You have also learned why organizing code into scripts is essential for reproducibility. Here, we build directly on this acquired knowledge by introducing the core concepts that govern how evaluates and executes code.
A command in is an instruction you give to the interpreter. Most commands are expressions, meaning they are pieces of code that evaluates to produce a result.
As earlier discussed, the simplest expressions in are arithmetic operations.
- Click
Run Codeto execute the examples.
Most of these operations behave exactly as expected. However, one notable difference is that there is no square-root symbol in . Instead, {fa brands r-project} uses functions such as sqrt() to compute roots.
When you run these expressions, , R evaluates them and prints the result to the Console. If you type them into a script and click Run (or press Ctrl + Enter↵ the same evaluation happens line by line.
You may notice that each printed result with [1]. This indicates the position (index) of the first element in the output. While this may seem odd now, it will make much more sense once we discuss vectors and indexing later on.
At this stage, the output results produced by your commands above are only temporary—once printed to the screen, they are lost (i.e., they are not stored anywhere). In order to perform anything beyond trivial calculations, you must be able to store results and reuse them when the need arises. In this is done through assignment.
An assignment takes the result of an expression and stores it as an object, using the assignment operator <-. The = operator can also be used for assignments, but it is less preferred for this purpose. Instead, it is mainly used for specifying arguments inside function calls. When an expression is assigned to an object, its value is no longer printed automatically to the Console.
- Run the object name
xto display its stored value.
- You can also explicitly use the
print()function to display output.
Once assigned, x becomes an object in your workspace. Objects are central to —they are how data, results, functions, and models are represented and manipulated.
- In almost everything is an object.
Be careful with spacing when using operators. for example:
This code does not assign a value to x. Instead, because of the space between the two characters that make up the assignment operator, interprets < as a logical comparison operator, that asks if x is less than -1 + 1 (i.e., zero). Although R generally ignores extra spaces, spaces must not split operators or object names. This, for example, is perfectly valid:
One important caveat to keep in mind is that silently overwrites any previous information stored in an object when you assign a new value to the same object name. For this reason, it’s a good practice to avoid reusing object names unless you intentionally want to replace their contents.
- Notice the value stored in object
ychanges because the second assignment replaces the first one.
In RStudio, assigned objects appear in the Environment pane (top right).
In VS Code and Positron, similar workspace panels show stored objects as below:
You can also list all stored objects programatically using:
To remove an object from the workspace, use rm() or remove():
- Attempting to access a removed object results in an
error.
remove()can also be used to remove a stored object from the workspace.
To remove all existing objects from the workspace, which is often recommended when starting a new analysis workflow, you can use the following command:
rm(list = ls())
ls()ls()lists all objects currently in the workspace.rm()removes objects.rm(list = ls())removes everything thatls()finds.- Running
ls()afterward confirms that the workspace is empty.
This helps ensure that your analysis starts from a clean environment and is not influenced by leftover objects from previous work.
If you want to assign the same value to multiple objects in a single statement:
is a case-sensitive language. This applies to object names, function names, and other identifiers in . If you create an object using a lowercase name, referring to it with a different case will result in an error.
For example, if an object is assigned to lowercase x, attempting to access it using uppercase X will give an error:
Similarly, function names must use the exact correct casing:
In , commands can be separated in two ways:
By starting a new line (i.e., hitting
Enter↵) orBy using a semicolon
;
Using semicolons can be useful for condensing code in scripts, although placing each command on its own line is generally clearer and more readable. If a command is incomplete on a line, either by design or due to a syntax error, the Console prompt changes from > to +, indicating that is waiting for additional input before it can evaluate the command.
Besides arithmetic operations there are also logical queries you can perform. Logical queries always evaluate either TRUE or FALSE. Here are a few examples to illustrate this.
Babuis a Hindu honorific to show respect or endearment.
Masoyiyatais a Hausa term of endearment meaning"my love"or"my beloved".
Logical comparisons play a crucial role in filtering data, making decisions, and controlling program flow.
The language provides a rich set of operators including those for arithmetic, comparison, logical evaluation, indexing, and model specification. The interactive table below summarizes some of the most commonly used operators. Feel free to explore them and their example usages.
To bring everything together, commands in operate on objects. An object is anything that can be assigned a name using the assignment operator <-. Objects can store values, data, or even behavior.
Common object types include vectors, matrices, factors, lists, data frames, and functions. With the exception of functions, these are often referred to as data structures or data objects. Each object type behaves differently, and operators interact with them in specific ways.
Understanding how objects are created, stored, and manipulated, and how operators act on them, is central to effective programming. This foundation prepares you to work confidently with data types and data structures, which we explore next.
4.2 Data types
A "data type" describes the kind of values stored in an object, not the structure that holds them. For example, a vector, data frame or list can store different data types. In , the most commonly encountered data types are numeric, character, logical, and complex. These basic types are sometimes referred to as modes.
Numeric data represent numbers and are used in calculations.
By default, numbers in are stored as numeric (double-precision) values. There is also an integer type, which we revisit later, but for most practical purposes numeric values behave the same in calculations.
Character data are used to store text.
When you enter character data directly in , you must enclose individual elements with either single or double quotes. If quotes are omitted, assumes you are referring to an existing object.
- An
erroroccurs becauseJohnis interpreted as an object name rather than text.
Single or double quotes are interchangeable but they must be used consistently. When character data are imported from files such as CSVs, quotes are typically handled automatically and do not need to be added manually.
Logical data represent truth values and can take only three possible values: TRUE, FALSE, and NA. The value NA indicates missing data, which is handled explicitly in {< fa brands r-project >}.
Note that logical values are not enclosed in quotes. Quoting them would convert them to
characterdata.The same is also true for numeric values.
Although recognizes T and F as short forms for TRUE and FALSE respectively, these are not reserved keywords and can therefore be reassigned by the user. For this reason, it is generally safer to use the full logical values.
As already illustrated in the previous section, logical values often arise as the result of comparisons and relational expressions.
Complex numbers consist of a real and an imaginary part and are written using i to denote the imaginary unit.
Complex data are less common in everyday data analysis, but they are supported natively by
You can examine the type of data stored in an object using the class() or mode() functions.
The class() function is generally preferred in modern programming because it reflects how objects behave in practice, especially for more complex data structures. The mode() function reports the underlying storage type and is mostly useful for low-level inspection.
In , you can also whether an object belongs to a particular data type using logical test functions. These functions follow the pattern is.<class>() fand always return either TRUE or FALSE, depending on the class of the object being tested.
Logical tests are especially useful when writing code that needs to behave differently depending on the type of data it receives. They help you confirm your assumptions about objects before performing further operations.
In addition to testing data types, it can sometimes be useful to be able to change the class of an object. allows you to change the class of an object using coercion functions. These functions follow the pattern as.<class>().
While coercion can be very useful, it should be used with care. Converting objects between classes does not always work as expected, particularly when the original data cannot be meaningfully represented in the new type.
Let us start by checking the class of x:
Coercing numeric values to character
- Notice that the number is now treated as text and appears enclosed in quotation marks.
In this case, the conversion is straightforward. Numeric values can always be represented as character strings, so no information is lost.
Now consider the opposite direction:
NAvalues are introduced because the character strings in name cannot be interpreted as valid numbers.
When encounters characters that cannot be converted into numeric values, it replaces them with NA and issues a warning. This behavior highlights why coercion should be done carefully, especially when working with real-world data.
The table below summarizes some commonly used logical test functions and their corresponding coercion functions in .
| Type | Logical test | Coercing |
|---|---|---|
| Character | is.character() | as.character() |
| Numeric | is.numeric() | as.numeric() |
| Logical | is.logical() | as.logical() |
| Factor | is.factor() | as.factor() |
| Complex | is.complex() | as.complex() |
- Always test a variable’s class before coercing it, especially when working with imported data.
4.3 Data structures
In , data are stored in data structures, sometimes also called data objects. These are the containers that hold your data and that you perform operations on, such as calculations, plotting and statistical analysis. Every object you create in exists in one of these structures.
The most common data structures in are vectors, matrices, arrays, data frames, lists, and factors. Each has a specific purpose and is suited to different kinds of tasks. In later sections, you will learn how to work with them in more depth. For now, the goal is to understand what they look like and how they differ.
Vectors are perhaps the most fundamental data structure in . A vector is an ordered collection of elements, such as numbers, characters, or logical values.
1:12creates a sequence of integers from 1 to 12.All elements in a vector must be of the same data type.
Vectors are the building blocks for many other data structures.
Matrices are similar to vectors, but they have two dimensions, rows and columns. Like vectors, matrices can only store one type of data at a time.
The
matrix()function reshapes a vector into rows and columns.nrow = 3tells how many rows the matrix should have.Values are filled column by column by default.
Arrays extend the idea of matrices by allowing more than two dimensions. They are useful when working with multidimensional data.
dim = c(2, 5, 3)defines rows, columns, and layers.Arrays still store only one data type.
Arrays are less common for beginners but appear in advanced applications.
Vectors, matrices, and arrays share the same rule: they can only store one data type at a time. This means that all elements must be numeric, character, or logical. This property is often referred to as having a single mode.
Data frames are one of the most commonly used data structures in . Like matrices, they are two-dimensional, but unlike matrices, each column can store a different data type.
Data frames resemble tables used in spreadsheets or other statistical software. Each column typically represents a variable, and each row usually represents an “observation”, or “record”, or “experimental unit”.
Each column is a
vectorof the same length.Different columns can have different data types.
Data framesare central to data analysis in .
Lists are flexible data structures that can store different types of objects, including other data structures. Unlike vectors, list elements do not need to be the same type or size.
Lists often appear as the output of functions, especially more complex ones.
A
listcan contain numbers, vectors, matrices, and data frames.Elements inside a
listcan have different structures.
Lists are essential for handling complex results.
A data structure does not need to contain data in order to exist. You can create an empty object using NULL. This can be useful when you want to prepare a container that will be filled later.
NULLrepresents the absence of data.Empty objects are often used in programming and iterative workflows.
Understanding data structures is essential for effective programming. The way data are stored determines how they behave when you manipulate them, how functions interact with them, and how results are returned. As you progress, you will see that most errors and unexpected behavior in R can be traced back to misunderstandings about data structures.
References
Reuse
Copyright
Citation
@online{nelson_kaunda2024,
author = {Nelson Kaunda, Elias},
title = {An {Introduction} to {R}},
date = {2024-09-23},
url = {https://elias-nelson.github.io/posts/intro-to-r/},
langid = {en}
}






























































































